<?php
// +----------------------------------------------------------------------
// | 志进科技
// +----------------------------------------------------------------------
// | 官方网站: http://www.zhijin101.com
// +----------------------------------------------------------------------
// | Author: 张亚伟 <1743325520@qq.com>
// +----------------------------------------------------------------------

namespace Api\Controller;
/**
 * 共享麻将机
 */
class MahjongController extends ApiController
{
    protected $noCheck = ['search', 'info'];

    function _initialize() {
        parent::_initialize();
    }

    /**
     * 共享麻将机列表
     * @Author   张亚伟<1743325520@qq.com>
     * @DateTime 2018-10-11
     */
    public function search()
    {
        $page = I('page', 1);
        $size = I('size', 20);
        $lat = I('lat');// 纬度
        $lng = I('lng');// 经度

        $map = [];
        $map['status'] = 1;

        $model = M('share_mahjong');
        $list = $model->where($map)->page($page, $size)->field('id,user_id,title,img,is_sales,page_view,address,lng,lat')->select();

        $amap_key = 'd7cdde040024dc1fe22157970ba6bd6b';
        if($list){
            if($lat && $lng){
                $origins = '';
                foreach ($list as $key => $row) {
                    if($row['lng'] && $row['lat']){
                        $origins.= ($origins ? "|":'').$row['lng'].','.$row['lat'];
                    }
                }
                if($origins){
                    $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key={$amap_key}&locations={$origins}&coordsys=baidu";
                    $res = curl_get($url);
                    $res = json_decode($res, true);

                    $origins = '';
                    if($res['status'] == '1'){
                        $locations = $res['locations'];
                        $arr = explode(';', $locations);
                        foreach ($arr as $key => $row) {
                            $origins.= ($origins ? "|":'').explode(',', $row)[0].','.explode(',', $row)[1];
                        }
                    }
                    $url = "http://restapi.amap.com/v3/assistant/coordinate/convert?key={$amap_key}&locations={$lng},{$lat}&coordsys=gps";
                    $res = curl_get($url);
                    $res = json_decode($res, true);      
                    // dump($res);      
                    if($res['status'] == '1'){
                        $lng = explode(',', $res['locations'])[0];
                        $lat = explode(',', $res['locations'])[1];
                    }
                    try{
                        require_once 'ThinkPHP/Library/Vendor/autoload.php';
                        $res = \yishuixm\lbs\LbsAmap::distance($amap_key, $origins, $lng.','.$lat, 1);
                        $res = json_decode($res, true);
                        if($res['status'] == 1){
                            $key_i = 0;
                            foreach ($list as $key => &$row) {
                                if($row['lng'] && $row['lat']){
                                    $row['distance'] = $res['results'][$key_i]['distance'];
                                    $key_i++;
                                }else{
                                    $row['distance'] = null;
                                }
                            }
                            array_multisort(array_column($list, 'distance'), SORT_ASC, $list);
                        }
                    }catch(\Exception $e){

                    }
                }
            }
            foreach ($list as $key => &$row) {
                $row['distance'] = isset($row['distance']) ? $row['distance'] : null;
                if($row['user_id'] && $row['user_id'] == $this->uid){
                    $row['is_del'] = 1;
                }else{
                    $row['is_del'] = 0;
                }
                unset($row['user_id']);
            }
        }
        $this->ajaxReturn(['s'=>true, 'm'=>'ok', 'list'=>$list]);
    }

    /**
     * 共享麻将机信息
     * @Author   张亚伟<1743325520@qq.com>
     * @DateTime 2018-10-11
     * @return   [type]                 [description]
     */
    public function info()
    {
        $id = I('id');
        $model = M('share_mahjong');
        $info = $model->where(['id'=>$id])->find();
        if(empty($info)){
            $this->ajaxReturn(['s'=>false, 'm'=>'信息不存在']);
        }
        if($info['share_num'] >= 3){
            $info['show_consult'] = 1;
        }else{
            $info['show_consult'] = 0;
        }
        if($this->uid){
            $is_share = M('share_log')->where(['user_id'=>$this->uid, 'scene'=>'mahjong', 'relation_id'=>$id])->find() ? 1 : 0;
        }else{
            $is_share = 0;
        }
        $warm_prompt = yzl_config('share_mahjong.detail_warm_prompt');
        $member_no = M('member_no')->where(['user_id'=>$info['user_id']])->getField('member_no');
        $info['member_no'] = $member_no;
        $model->where(['id'=>$id])->setInc('page_view', 1);
        $this->ajaxReturn(['s'=>true, 'm'=>'ok', 'info'=>$info, 'is_share'=>$is_share, 'warm_prompt'=>$warm_prompt]);
    }

    /**
     * 删除共享麻将机
     * @Author   张亚伟<1743325520@qq.com>
     * @DateTime 2018-10-11
     * @return   [type]                 [description]
     */
    public function del()
    {
        $id = I('id');
        $model = M('share_mahjong');
        $res = $model->where(['id'=>$id, 'user_id'=>$this->uid])->delete();
        if($res){
            $this->ajaxReturn(['s'=>true, 'm'=>'删除成功']);
        }else{
            $this->ajaxReturn(['s'=>false, 'm'=>'删除失败']);
        }
    }

    public function add_config()
    {
        $warm_prompt = yzl_config('share_mahjong.warm_prompt');
        $config = [
            'warm_prompt'   => $warm_prompt,
        ];
        $this->ajaxReturn(['s'=>true, 'm'=>'ok', 'config'=>$config]);
    }

    /**
     * 创建共享麻将机
     * @Author   张亚伟<1743325520@qq.com>
     * @DateTime 2018-10-11
     * @return   [type]                 [description]
     */
    public function add()
    {
        $model = M('share_mahjong');
        $count = $model->where(['user_id'=>$this->uid])->count();
        if($count >=3 ){
            $this->ajaxReturn(['s'=>false, 'm'=>'最多添加3条']);
        }
        $data = I('post.');
        
        // $data['images'] = $this->uploadimgsy('images');
        $data['img'] = !empty($data['images']) ? explode(',', $data['images'])[0] : '';
        $data['user_id'] = $this->uid;
        $data['create_time'] = time();
        $data['update_time'] = time();
        $id = $model->add($data);
        if($id){
            $this->ajaxReturn(['s'=>true, 'm'=>'添加成功']);
        }else{
            $this->ajaxReturn(['s'=>false, 'm'=>'添加失败']);
        }
    }
}